package simpleGui;

import java.awt.ItemSelectable;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.util.Vector;

import processing.core.PGraphics;

public class GuiStateButton extends GuiComponent implements ItemSelectable {
	boolean over;
	public int state;
	public int maxState;
	Vector<ItemListener>itemListener=new Vector<ItemListener>();
	public GuiStateButton(Gui gui, int x, int y, int w, int h,int state,int maxState) {	
		this.gui=gui;
		this.setDimensions(x, y, w, h);
		this.state=state;
		this.maxState=maxState;
		gui.parent.addMouseListener(this);
		displayLabel=true;
	}
	public GuiStateButton(Gui gui, int x, int y, int w, int h,int state,int maxState,String label) {	
		this(gui,x,y,w,h,state,maxState);
		this.label=label;
		displayLabel=true;
	}
	
	public void setValue(int mx,int my){
		if (contains(mx,my)){
			state++;
			state=state%maxState;
		}
	}
	
	
	
	public void display(PGraphics g) {
		g.pushStyle();
		
		g.fill(state*255/maxState,0,0);
		
		g.strokeWeight(gui.getStrokeWeight());
		g.stroke(gui.getStrokeColor());
		g.rect(x, y, w, h);
		g.fill(gui.getTextColor());
		if (label!=null&&this.displayLabel){
		g.text(label, x+gui.getPadding(), y+h-gui.getPadding());
		}
		g.popStyle();
		displayChildren(g);
	}


	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		setValue(arg0.getX(),arg0.getY());
	}


	@Override
	public void addItemListener(ItemListener arg0) {
		// TODO Auto-generated method stub
		itemListener.add(arg0);
	}



	@Override
	public void removeItemListener(ItemListener arg0) {
		// TODO Auto-generated method stub
		itemListener.remove(arg0);
	}
	@Override
	public Object[] getSelectedObjects() {
		// TODO Auto-generated method stub
		return null;
	}
	public int getState() {
		return state;
	}
	public void setState(int state) {
		this.state = state;
	}
}
